__author__ = 'Totomo'
# -*- coding:utf-8 -*-
# 我爆炸用了自己的图片，在Question4的image文件夹下

import pygame
from pygame.locals import *  # pygame使用的各种常量
import time, random


# 放置玩家英雄飞机，并绑定键盘事件，实现飞机移动
class HeroPlane:
    """
    玩家飞机类（英雄）
    """

    def __init__(self, screen_temp):
        self.x = 206
        self.y = 750
        self.screen = screen_temp
        self.image = pygame.image.load("./images/me.png")
        # 存储发射出去的子弹对象引用
        self.bullet_list = []

    def display(self,enemylist):
        """
        绘制玩家到窗口中
        """
        # 遍历移动子弹
        for bullet in self.bullet_list:
            bullet.display()
            # 移动子弹，并判断是否越界。
            if bullet.move():
                self.bullet_list.remove(bullet)

        # 遍历敌机
        for en in enemylist:
            # 遍历敌人的子弹，并做碰撞检测
            for bo in en.bullet_list:
                if self.x + 12 < bo.x < self.x + 92 and self.y + 20 < bo.y < self.y + 60:
                    en.bullet_list.remove(bo)
                    return True
        self.screen.blit(self.image, (self.x, self.y))

    def move_left(self):
        """
        左移动,并判断防止越界
        """
        self.x -= 5
        if self.x < 0:
            self.x = 0
        # 遍历敌机

    def move_right(self):
        """
        右移动,并判断防止越界
        """
        self.x += 5
        if self.x > 406:
            self.x = 406

    def fire(self):
        """
        开火，绘制子弹
        """
        self.bullet_list.append(Bullet(self.screen, self.x, self.y))
        print(len(self.bullet_list))


class Bullet:
    """
    玩家子弹类
    """

    def __init__(self, screen_temp, x, y):
        self.x = x + 53
        self.y = y
        self.screen = screen_temp
        self.image = pygame.image.load("./images/pd.png")

    def display(self):
        self.screen.blit(self.image, (self.x, self.y))

    def move(self):
        self.y -= 10
        if self.y < -20:
            return True


class Bullet_E:
    """
    敌机子弹类
    """

    def __init__(self, screen_temp, x, y, speed):
        self.screen = screen_temp
        self.image = pygame.image.load("./images/pd.png")
        self.speed = speed

        # 根据speed也就是敌机种类设置初始化子弹位置,敌机大小 0号机(12) 1号机(9) 2号机(5)
        if self.speed == 12:
            self.x = x + 56
            self.y = y + 65
        else:
            self.x = x + 48
            self.y = y + 65

    def display(self):
        self.screen.blit(self.image, (self.x, self.y))

    def move(self):
        self.y += self.speed
        if self.y > 920:
            return True


class EnemyPlanes:
    """
    敌机的类
    """

    def __init__(self, screen_temp, speed):
        self.x = random.choice(range(408))
        self.y = -75
        self.screen = screen_temp
        self.speed = speed
        self.image = pygame.image.load("./images/e" + str(self.speed) + ".png")
        self.bullet_list = []

    def display(self):
        """
        绘制敌机到窗口中
        """
        # 遍历移动子弹
        if random.choice(range(20)) == 1:
            self.fire()
        for bullet in self.bullet_list:
            bullet.display()
            # 移动子弹，并判断是否越界。
            if bullet.move():
                self.bullet_list.remove(bullet)
        self.screen.blit(self.image, (self.x, self.y))

    def move(self, hero):
        """
        敌机自动移动
        """
        self.y += 4
        # 遍历玩家的子弹，并做碰撞检测
        for bo in hero.bullet_list:
            if self.x + 12 < bo.x < self.x + 92 and self.y + 20 < bo.y < self.y + 60:
                hero.bullet_list.remove(bo)
                return True
        # 判断敌机是否越界
        if self.y > 900:
            return True

    def fire(self):
        """
        敌机开火
        """
        # 根据不同敌机设定子弹速度
        if self.speed == 0:
            self.bullet_list.append(Bullet_E(self.screen, self.x, self.y, 12))
        elif self.speed == 1:
            self.bullet_list.append(Bullet_E(self.screen, self.x, self.y, 9))
        else:
            self.bullet_list.append(Bullet_E(self.screen, self.x, self.y, 5))


def key_control(hero_temp):
    """
    键盘控制函数
    """
    # 执行退出操作
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            print("exit")
            exit()

    # 获取按下的键(返回的是元组值)
    pressed_keys = pygame.key.get_pressed()
    # 检测是否按下a或者left键
    if pressed_keys[pygame.K_LEFT] or pressed_keys[pygame.K_a]:
        hero_temp.move_left()
    # 检测是否按下d或者right键
    elif pressed_keys[pygame.K_RIGHT] or pressed_keys[pygame.K_d]:
        hero_temp.move_right()
    # 检查是否是空格键
    if pressed_keys[pygame.K_SPACE]:
        print('fire!')
        hero_temp.fire()

class Boom:
    def __init__(self, screen_temp, plane_x, plane_y):
        self.x = plane_x
        self.y = plane_y
        self.screen = screen_temp

    def oops(self):
        img_o = pygame.image.load("./images/bomb.png")
        self.screen.blit(img_o, (self.x, self.y))
        time.sleep(0.2)
        img_o = pygame.image.load("./images/bomb2.png")
        self.screen.blit(img_o, (self.x, self.y))


def main():
    """
    主程序函数
    """
    bc1 = 1536
    bc2 = 900

    pygame.init()
    # 创建主窗口,pygame.NOFRAME是无边框,set_mode(分辨率=(0,0),标志=0,深度=0)
    screen = pygame.display.set_mode((512, bc2), 0, 0)

    # 创建游戏背景(512*1536)
    background = pygame.image.load("./images/bg2.jpg")
    background1 = pygame.image.load("./images/bg2.jpg")

    # 创建一个玩家飞机对象
    hero = HeroPlane(screen)

    # 定义用于存放敌机列表
    enemylist = []

    # 背景定时移动并且无缝连接
    # 初始化游戏背景图片标轴y的值
    m = bc2 - bc1
    m1 = 0 - bc1 + m
    while True:
        # 绘制画面
        screen.blit(background, (0, m))
        screen.blit(background1, (0, m1))
        m += 2
        m1 += 2
        if m >= 900:
            m = bc2 - bc1
            m1 = 0 - bc1 + m

        # 显示英雄玩家，如与子弹碰撞则爆炸
        if hero.display(enemylist):
            print("ooooooooops!")
            exit()
        else:
            hero.display(enemylist)

        # 随机输出敌机
        # 1/100的概率出0号机，1/75的概率出1号机，1/50的概率出2号机
        if random.choice(range(100)) == 1:
            enemylist.append(EnemyPlanes(screen, 0))
        elif random.choice(range(75)) == 1:
            enemylist.append(EnemyPlanes(screen, 1))
        elif random.choice(range(50)) == 1:
            enemylist.append(EnemyPlanes(screen, 2))
        # 遍历所有敌机，显示敌机，移动敌机，并与玩家子弹碰撞检测
        for em in enemylist:
            em.display()
            if em.move(hero):
                enemylist.remove(em)
                boom = Boom(screen,em.x,em.y)
                boom.oops()

        key_control(hero)

        # 更新显示
        pygame.display.update()

        # 定时显示
        time.sleep(0.04)

# 判断当前是否是主运行程序，并调用
if __name__ == "__main__":
    main()
